package com.sky.controller.user;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.result.Result;
import com.sky.service.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @description
 * @author: ZXX-挚爱
 * @time: 2023/6/12 15:53
 */
@RestController
@RequestMapping("/user/addressBook")
@Api(tags = "地址薄管理模块")
public class AddressBookController {

    @Autowired
    private AddressBookService addressBookService;

    /** @author ZXX-挚爱
    * @Description 新增地址薄功能接口
    * @Dete 16:12 2023/6/12
    * @Param [addressBook]
    * @return com.sky.result.Result
    **/

    @PostMapping
    @ApiOperation("新增地址薄功能接口")
    public Result SaveAddressBook(@RequestBody AddressBook addressBook){
        addressBookService.save(addressBook);
        return Result.success();
    }

    /** @author ZXX-挚爱
    * @Description 查询当前登录用户的所有地址信息
    * @Dete 16:44 2023/6/12
    * @Param
    * @return com.sky.result.Result<java.util.List<com.sky.entity.AddressBook>>
    **/
    
    @GetMapping("/list")
    @ApiOperation("查询当前登录用户的所有地址信息功能接口")
    public Result<List<AddressBook>> showAddressBook(){
        AddressBook addressBook = new AddressBook();
        addressBook.setUserId(BaseContext.getCurrentId());
        List<AddressBook> addressBookList=addressBookService.showAddressBook(addressBook);
        return Result.success(addressBookList);
    }

    /** @author ZXX-挚爱
    * @Description 查询默认地址功能接口
    * @Dete 17:53 2023/6/12
    * @Param
    * @return com.sky.result.Result<com.sky.entity.AddressBook>
    **/

    @GetMapping("/default")
    @ApiOperation("查询默认地址功能接口")
    public  Result<AddressBook> getDefault(){
        AddressBook addressBook = new AddressBook();
        addressBook.setIsDefault(1);
        addressBook.setUserId(addressBook.getUserId());
        List<AddressBook> addressBookList= addressBookService.getDefaultAddress(addressBook);
        if (addressBookList!=null &&addressBookList.size()==1){
            return Result.success(addressBookList.get(0));
        }
        return Result.error("没有查询到默认地址");
    }

    /** @author ZXX-挚爱
    * @Description 根据id修改地址功能接口
    * @Dete 18:03 2023/6/12
    * @Param [addressBook]
    * @return com.sky.result.Result
    **/

    @PutMapping
    @ApiOperation("根据id修改地址功能接口")
    public Result update(@RequestBody AddressBook addressBook){
        addressBookService.update(addressBook);
        return Result.success();

    }
    
    /** @author ZXX-挚爱
    * @Description 根据id查询地址
    * @Dete 19:56 2023/6/12
    * @Param [id]
    * @return com.sky.result.Result<com.sky.entity.AddressBook>
    **/

    @GetMapping("/{id}")
    @ApiOperation("根据id查询地址")
    public Result<AddressBook> getById(@PathVariable Long id){
        AddressBook addressBook=addressBookService.queryById(id);
        return Result.success(addressBook);
    }
    /** @author ZXX-挚爱
    * @Description 根据id删除地址
    * @Dete 20:04 2023/6/12
    * @Param [id]
    * @return com.sky.result.Result
    **/

    @DeleteMapping
    @ApiOperation("根据id删除地址")
    public Result deleteById(Long id){
        addressBookService.deleteById(id);
        return Result.success();
    }

    /** @author ZXX-挚爱
    * @Description 设置默认地址接口
    * @Dete 20:05 2023/6/12
    * @Param [addressBook]
    * @return com.sky.result.Result
    **/

    @PutMapping("/default")
    @ApiOperation("设置默认地址接口")
    public Result setDefault(@RequestBody AddressBook addressBook){
        addressBookService.setDefaultAddress(addressBook);
        return Result.success();
    }
}
